<?php
namespace Admin\Controller;

class WeixinController extends PublicController {
	function _initialize() {
		parent::_initialize ();
	}

	public function index() {
        $store_id = session("wadmin");
        if ($store_id['store_id'] == 0) {
            $map = '';
            $storeId = 0;
        } else {
            $map['store_id'] = $store_id['store_id'];
            $storeId = $store_id['store_id'];
        }

		$config = M ( "Wxconfig" )->where($map)->find ();
		$store = M ( "store" )->select();
		$this->assign ( "config", $config );
		$this->assign ( "store", $store );
		$this->assign ( "storeId", $storeId );
		$this->assign ( "url", 'http://' . $_SERVER ['HTTP_HOST'] . U('Admin/Wechat/index') );

		$this->display ();
	}

    /**
     * 编辑基本信息
     */
	public function setconfig() {
	    if(I("post.") && is_array(I("post."))){
            $id = I("post.id");
            $map =  I("post.");
            $store_id = session("wadmin");
            if ($store_id['store_id']==0) {
                $map['store_id'] = I("post.store_id"); //获取的店铺ID
            } else {
                $map['store_id'] = $store_id['store_id']; //储存的店铺ID
            }
            if ($id) {
                $result = M("Wxconfig")->where("id=".$id)->save( $map);
            } else {
                $result = M("Wxconfig")->add( $map);
            }
            $this->success ( "配置成功!" );
        }else{
            $this->error("输入正确的参数");
        }
	}

	public function addmenu() {
		$data = I("post.");
        $store_id = session("wadmin");
        $data['store_id'] = $store_id['store_id']; // 店铺ID
		if ( $data["id"]) { //修改
			$data["status"] = '1';
            $id = $data["id"];
			$result = M ( "Wxmenu" )->where("id='$id'")->save ( $data );
			if ($result !== false) {
				$this->success ( "修改菜单成功!" ,U('Admin/Weixin/menu_list'));
			}else{
                $this->error("修改失败");
            }
		} else { // 新增
			$data["status"] = '1';
			unset ( $data["id"] );
			$result = M ( "Wxmenu" )->add ( $data );
			if ($result !== false) {
				$this->success ( "添加菜单成功!" ,U('Admin/Weixin/menu_list'));
			}else{
                $this->error("修改失败");
            }
		}
	}

	public function menu_list()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $data = '';
        } else {
            $data['w.store_id'] = $store_id['store_id']; // 店铺ID
        }

        $menu = M ( "Wxmenu" )->alias("w")->join("meal_store as s on w.store_id=s.store_id")->field("w.*,s.store_name")->where($data)->select ();
        $this->assign ( "menu", $menu );
        $this->display ();
    }

    public function add_menu()
    {
        $this->display ();
    }

    /**
     *菜单编辑
     */
    public function menu_edit()
    {
        $id = I("get.id");
        //根据id查询信息
        $menu = M("Wxmenu")->where(array(
            "id" => $id
        ))->find();

        $this->assign ( "menu", $menu );
        $this->display ();
    }

	public function Menu(){
        $this->display ();
    }

	public function delmenu() {
		$id = I("get.id");
		$result = M ( "Wxmenu" )->where ( array (
				"id" => $id 
		) )->delete ();
		if ($result) {
			$this->success ( "删除菜单成功!" );
		}
	}

    /**
     * 删除自动回复
     */
    public function delReply() {
        $id = I("get.id");
        $result = M ( "Wxmessage" )->where ( array (
            "id" => $id
        ) )->delete ();
        if ($result) {
            $this->success ( "删除自动回复成功!" );
        }
    }


    /**
     * 自动回复列表
     */
	public function reply_list()
    {
        $message = M ( "Wxmessage" )->select ();

        $this->assign ( "message", $message );
        $this->display ();
    }

    /**
     * 新增自动回复
     */
    public function add_reply()
    {
        $this->display ();
    }

    /**
     * 编辑自动回复
     */
    public function reply_edit()
    {
        $id = I("get.id");
        //根据id查询信息
        $message = M("Wxmessage")->where(array(
            "id" => $id
        ))->find();

        $this->assign ( "message", $message );
        $this->display ();
    }

	public function addmessage() {
		$data = I("post.");
        if($_FILES['picurl']['name'] !== ''){
            $upload = $this->uploadOne($_FILES['picurl']);
            $data['picurl'] = ltrim($upload[savepath], ".").$upload[savename];
        }else{
            $data['picurl'] = $data['pic'];
        }

        if (I("post.message_id") == 0) {
            unset ( $data ["message_id"] );
            $result = M ( "Wxmessage" )->add ( $data );
        } else {
            $id = $data ["message_id"];
            $data ["id"] = $data ["message_id"];
            unset ( $data ["message_id"] );
            $result = M("Wxmessage")->where("id='$id'")->save($data);
        }

        if ($result === false) {
            $this->error("操作失败");
        }else{
            $this->success ( "操作成功!" ,U('Admin/Weixin/reply_list'));
        }
	}
	public function delmessage(){
		$result = M("Wxmessage")->where(array("id"=>I("get.id")))->delete();
		if ($result) {
			$this->success("删除成功!");
		}
	}
}
